package com.markerhub.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.DeviceInfo;
import com.markerhub.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Date;

/**
 * 设备信息Controller
 */
@Api(tags = "设备信息管理")
@RestController
@RequestMapping("/api/device")
public class DeviceInfoController extends BaseController {

    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
     * 获取设备信息列表 - 分页
     */
    @ApiOperation(value = "获取设备列表", notes = "获取所有设备信息，支持分页查询")
    @PreAuthorize("hasAuthority('device:list')")
    @GetMapping("/list")
    public Result list(DeviceInfo deviceInfo) {
        Page<DeviceInfo> page = deviceInfoService.page(getPage(), new QueryWrapper<DeviceInfo>()
                .like(deviceInfo.getDeviceName() != null, "device_name", deviceInfo.getDeviceName())
                .like(deviceInfo.getDeviceCode() != null, "device_code", deviceInfo.getDeviceCode())
                .eq(deviceInfo.getStatus() != null, "status", deviceInfo.getStatus()));
        return Result.succ(200, "获取设备信息列表成功", page);
    }

    /**
     * 查询设备 - 分页
     */
    @ApiOperation(value = "查询设备", notes = "根据条件查询设备信息，支持分页")
    @PreAuthorize("hasAuthority('device:query')")
    @GetMapping("/query")
    public Result query(DeviceInfo deviceInfo) {
        return list(deviceInfo);
    }

    /**
     * 根据ID获取设备信息
     */
    @ApiOperation(value = "获取设备详情", notes = "根据设备ID获取指定设备的详细信息")
    @PreAuthorize("hasAuthority('device:list')")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id) {
        DeviceInfo device = deviceInfoService.getById(id);
        return Result.succ(200, "获取设备信息成功", device);
    }

    /**
     * 创建设备信息
     */
    @ApiOperation(value = "新增设备", notes = "新增设备信息")
    @PreAuthorize("hasAuthority('device:add')")
    @PostMapping("/add")
    public Result add(@Validated @RequestBody DeviceInfo device) {
        // 设置创建时间
        device.setCreated(new Date());
        // 设置更新时间
        device.setUpdated(new Date());
        boolean saved = deviceInfoService.save(device);
        return saved ? Result.succ(200, "创建设备信息成功", device) : Result.fail("创建设备信息失败");
    }

    /**
     * 更新设备信息
     */
    @ApiOperation(value = "更新设备", notes = "更新指定设备的详细信息")
    @PreAuthorize("hasAuthority('device:update')")
    @PutMapping("/update")
    public Result update(@Validated @RequestBody DeviceInfo device) {
        // 设置更新时间
        device.setUpdated(new Date());
        boolean updated = deviceInfoService.updateById(device);
        return updated ? Result.succ(200, "更新设备信息成功", device) : Result.fail("更新设备信息失败");
    }

    /**
     * 删除单个设备信息
     */
    @ApiOperation(value = "删除设备", notes = "删除指定ID的设备信息")
    @PreAuthorize("hasAuthority('device:delete')")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean deleted = deviceInfoService.removeById(id);
        return deleted ? Result.succ(200, "删除设备信息成功", null) : Result.fail("删除设备信息失败");
    }

    /**
     * 批量删除设备信息
     */
    @ApiOperation(value = "批量删除设备", notes = "批量删除多个设备信息")
    @PreAuthorize("hasAuthority('device:delete')")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        boolean deleted = deviceInfoService.removeByIds(Arrays.asList(ids));
        return deleted ? Result.succ(200, "批量删除设备信息成功", null) : Result.fail("批量删除设备信息失败");
    }
}